package com.xexun.gpstracker.util;

import android.util.Log;

import org.json.JSONObject;
import org.xutils.common.Callback;

import com.xexun.gpstracker.AppContext;
import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.inter.HttpCallback;
import com.xexun.gpstracker.pojo.ResponseData;

/**
 * Created by HH on 2015/8/20.
 *
 */
public class JsonHttpResponseHelper {
    private HttpCallback callback;

    public JsonHttpResponseHelper(HttpCallback callback) {
        this.callback = callback;
    }

    public Callback.ProgressCallback<JSONObject> getHandler() {
        return new Callback.ProgressCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                try {
                    Log.d(JsonHttpResponseHelper.class.getName(),result.toString());
                    ResponseData responseData = new ResponseData(result);
                    if (responseData.getRetcode() == 2) {
                        //如果超时，则直接退出到登录界面。
                        CommUtil.showMsgShort(responseData.getMsg());
                        AppContext.getEventBus().post(new Object(), Constants.EVENT_TAG_OFFLINE_NOTIFY);

                        return;
                    }
                    callback.onSuccess(responseData);
                } catch (Exception e) {
                    Log.w(JsonHttpResponseHelper.class.getName(), AppContext.getContext().getString(R.string.callback_err), e);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                try {
                    callback.onFailure(Constants.DEFAULT_BLANK);
                } catch (Exception e) {
                    Log.w(JsonHttpResponseHelper.class.getName(), AppContext.getContext().getString(R.string.callback_err), e);
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                try {
                    callback.onFinish();
                } catch (Exception e) {
                    Log.w(JsonHttpResponseHelper.class.getName(), AppContext.getContext().getString(R.string.callback_err), e);
                }
            }

            @Override
            public void onWaiting() {

            }

            @Override
            public void onStarted() {
                try {
                    callback.onStart();
                } catch (Exception e) {
                    Log.w(JsonHttpResponseHelper.class.getName(), AppContext.getContext().getString(R.string.callback_err), e);
                }
            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                try {
                    callback.onLoading(total, current, isDownloading);
                } catch (Exception e) {
                    Log.w(JsonHttpResponseHelper.class.getName(), AppContext.getContext().getString(R.string.callback_err), e);
                }
            }
        };
    }
}
